package com.intelligent.health.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/04/06/22:48
 * @Description: gateway 路由转发
 */
@Configuration
public class RoutesConfiguration {

    @Autowired
    private KeyResolver hostAddrKeyResolver;

    @Autowired
    @Qualifier("setMealServiceRateLimiter")
    private RateLimiter setMealServiceRateLimiter;

    @Autowired
    @Qualifier("memberServiceRateLimiter")
    private RateLimiter memberServiceRateLimiter;

    @Autowired
    @Qualifier("oauthServiceRateLimiter")
    private RateLimiter oauthServiceRateLimiter;

    @Autowired
    @Qualifier("baseServiceRateLimiter")
    private RateLimiter baseServiceRateLimiter;

    @Autowired
    @Qualifier("quartzServiceRateLimiter")
    private RateLimiter quartzServiceRateLimiter;

    @Bean
    public RouteLocator declare(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(route -> route.path("/oauth-service/**")
                        .filters(f -> f.stripPrefix(1)
                                .requestRateLimiter(limiter-> {
                                            limiter.setKeyResolver(hostAddrKeyResolver);
                                            limiter.setRateLimiter(oauthServiceRateLimiter);
                                            limiter.setStatusCode(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
                                        }
                                )
                        )
                        .uri("lb://health-oauth2-server")
                ).route(route -> route.path("/quartz-service/**")
                        .filters(f -> f.stripPrefix(1)
                                .requestRateLimiter(limiter-> {
                                            limiter.setKeyResolver(hostAddrKeyResolver);
                                            limiter.setRateLimiter(quartzServiceRateLimiter);
                                            limiter.setStatusCode(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
                                        }
                                )
                        )
                        .uri("lb://health-quartz-serv")
                ).route(route -> route.path("/base-service/**")
                        .filters(f -> f.stripPrefix(1)
                                .requestRateLimiter(limiter-> {
                                            limiter.setKeyResolver(hostAddrKeyResolver);
                                            limiter.setRateLimiter(baseServiceRateLimiter);
                                            limiter.setStatusCode(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
                                        }
                                )
                        )
                        .uri("lb://health-base-serv")
                ).route(route -> route.path("/member-service/**")
                        .filters(f -> f.stripPrefix(1)
                                .requestRateLimiter(limiter-> {
                                            limiter.setKeyResolver(hostAddrKeyResolver);
                                            limiter.setRateLimiter(memberServiceRateLimiter);
                                            limiter.setStatusCode(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
                                        }
                                )
                        )
                        .uri("lb://health-member-serv")
                ).route(route -> route
                        // 如果一个请求命中了多个路由，order越小的路由优先级越高
                        .order(1)
                        .path("/setmeal-service/**")
                        .filters(f -> f.stripPrefix(1)
                                .requestRateLimiter(c-> {
                                            c.setKeyResolver(hostAddrKeyResolver);
                                            c.setRateLimiter(setMealServiceRateLimiter);
                                            c.setStatusCode(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED);
                                        }
                                )
                        )
                        .uri("lb://health-setmeal-serv")
                ).route(route -> route
                                .path("/base-service/**")
                                .filters(f -> f.stripPrefix(1))
                                .uri("lb://health-base-serv")

//                )
//                .route("id-001", route -> route
//                        // 在指定时间之前
//                        .before(ZonedDateTime.parse("2022-12-25T14:33:47.789+08:00"))
//                        // 在指定时间之后
//                        .or().after(ZonedDateTime.parse("2022-12-25T14:33:47.789+08:00"))
//                        // 或者在某个时间段以内
//                        .or().between(
//                                ZonedDateTime.parse("2021-12-25T14:33:47.789+08:00"),
//                                ZonedDateTime.parse("2022-12-25T14:33:47.789+08:00"))
//                        .uri("http://time.geekbang.org")
//                ).route(route -> route
//                        .path("/test/**")
//                        .uri("http://www.test.com")
                ).build();

    }

}
